package date;

import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.ParsePosition;
import java.util.Date;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

public class JodaTimeFormat extends DateFormat {

	private static final long serialVersionUID = -3121650569779268081L;

	private String pattern;

	public JodaTimeFormat(String pattern) {
		this.pattern = pattern;
	}

	@Override
	public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
		toAppendTo.append(new DateTime(date.getTime()).toString(pattern));
		return toAppendTo;
	}

	@Override
	public Date parse(String source, ParsePosition pos) {
		pos.setIndex(1);
		return DateTimeFormat.forPattern(pattern).parseDateTime(source).toDate();
	}

}
